package com.jj.entity;

import com.jj.battle.protocol.GameProto;
import com.jj.core.utils.Hex;
import com.jj.message.PBMessageFactory;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

@Slf4j
@Data
public class PlayerOperation {
    long playerID;
    int type;
    int direction;
    int eventID;
    int maxChargeFrame;
    int chargeFrames;
    int key;

    public static PlayerOperation fromMsg(GameProto.PKG_OPERATION msg, long playerID){
        PlayerOperation playerOperation = new PlayerOperation();

        playerOperation.setType(msg.getOPERATIONTYPE());

//        playerOperation.setDirection(msg.getInputDirection().getDirection());
//
//        playerOperation.setEventID(msg.getInputEvents().getEventID());
//        playerOperation.setMaxChargeFrame(msg.getInputEvents().getMaxChargeFrames());
//        playerOperation.setChargeFrames(msg.getInputEvents().getChargeFrames());
//        playerOperation.setKey(msg.getInputEvents().getKey());

        playerOperation.setPlayerID(playerID);
        return playerOperation;
    }

    public GameProto.DT_FRAME_CONTENT toContent(){

        GameProto.DT_INPUT_EVENT.Builder eventBuilder = GameProto.DT_INPUT_EVENT.newBuilder();
        eventBuilder.setEventID(getEventID());
        eventBuilder.setChargeFrames(getChargeFrames());
        eventBuilder.setMaxChargeFrames(getMaxChargeFrame());
        eventBuilder.setKey(getKey());

        GameProto.DT_INPUT_DIRECTION.Builder directionBuilder = GameProto.DT_INPUT_DIRECTION.newBuilder();
        directionBuilder.setDirection(getDirection());

        GameProto.PKG_OPERATION.Builder opBuilder = GameProto.PKG_OPERATION.newBuilder();
        opBuilder.setOPERATIONTYPE(getType());
//        opBuilder.setInputDirection(directionBuilder.build());
//        opBuilder.setInputEvents(eventBuilder.build());

        GameProto.DT_FRAME_CONTENT.Builder builder = GameProto.DT_FRAME_CONTENT.newBuilder();
        builder.setPlayerId(getPlayerID());
        builder.setOperation(opBuilder.build());

        return builder.build();
    }

    public static GameProto.PKG_FRAME add2Frame(GameProto.PKG_FRAME.Builder frameBuilder, List<PlayerOperation> playerOperations){
        playerOperations.forEach(playerOperation -> frameBuilder.addOperations(playerOperation.toContent()));
        return frameBuilder.build();
    }

    public static void main(String[] args) {
        PBMessageFactory pbMessageFactory = new PBMessageFactory();
        pbMessageFactory.init();
        PlayerOperation playerOperation = new PlayerOperation();
        playerOperation.setPlayerID(10000L);
        playerOperation.setKey(999);
        playerOperation.setChargeFrames(10);
        playerOperation.setMaxChargeFrame(8888888);
        playerOperation.setType(2);
        playerOperation.setDirection(-1);
        playerOperation.setEventID(77);

        GameProto.DT_FRAME_CONTENT content = playerOperation.toContent();
        int size = content.getSerializedSize();

        ByteArrayOutputStream bao = new ByteArrayOutputStream(size);
        try {
            content.writeTo(bao);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        byte[] raw = bao.toByteArray();
        log.debug("raw:\n{}", Hex.printhex2Buf(raw));
        log.debug("pb:{}", content.getPlayerId());
        log.debug("pb:{}", content.getOperation());

    }
}
